package hbase.phoenix;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;

public class ConnectPhoenix {

    public static void main(String[] args) {

        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            //定义与Phoenix连接的url
            String url = "jdbc:phoenix:node101,node102,node103:2181";

            Properties props = new Properties();
            props.put("phoenix.schema.isNamespaceMappingEnabled","true");
            //获取连接
            conn = DriverManager.getConnection(url,props);

            ps = conn.prepareStatement("upsert into student2 values(?,?,?)");
            ps.setString(1,"003");
            ps.setString(2,"rose");
            ps.setInt(3,20);

            int result = ps.executeUpdate();
            if(result==1){
                System.out.println("添加或修改成功");
            }else{
                System.out.println("添加或修改失败");
            }

            conn.commit();

            //执行查询操作
//            ps = conn.prepareStatement("select * from student2");
//
//            rs = ps.executeQuery();
//
//            while(rs.next()){
//                System.out.println("id:"+rs.getString(1));
//                System.out.println("name:"+rs.getString(2));
//                System.out.println("age:"+rs.getString(3));
//                System.out.println("================================");
//            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if(rs!=null){
                    rs.close();
                }
                if(ps!=null){
                    ps.close();
                }
                if(conn!=null){
                    conn.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }

        }

    }

}
